#!/bin/sh

# Kill parent elxhbamgr
ppid=`ps -eaf | grep elxhbamgr | grep -v grep | awk '{ print $2 }' | sort -n | head -1`
if [ ! -z "$ppid" ];then
        echo "Stopping HBAnyware HBA Management Server"
	kill $ppid > /dev/null 2>&1
fi

# Kill any remaining child of elxhbamgr
for ppid in `ps -eaf | grep elxhbamgr | grep -v grep | awk '{ print $2 }' | sort -n`
do
	kill -9 $ppid > /dev/null 2>&1
done


# Kill HBAnyware and SSC GUIs

# Get pid of script that started HBAnyware GUI
script_pid=`ps -eaf | grep hbanyware | grep -v grep | head -1 | awk '{ print $2 }'`

if [ "$script_pid" != "" ];then
#  Get pid of GUI, child process of script_pid
   gui_pid=`ps -eaf | grep java | grep -v grep | grep $script_pid | awk '{ print $2 }'`
   if [ "$gui_pid" != "" ];then
      echo "Stopping HBAnyware GUI"
#     Kill HBAnyware GUI
      kill $gui_pid > /dev/null 2>&1
   fi
fi

# Get pid of script that started SSC GUI
script_pid=`ps -eaf | grep ssc | grep -v grep | head -1 | awk '{ print $2 }'`

if [ "$script_pid" != "" ];then
#  Get pid of GUI, child process of script_pid
   gui_pid=`ps -eaf | grep java | grep -v grep | grep $script_pid | awk '{ print $2 }'`
   if [ "$gui_pid" != "" ];then
      echo "Stopping HBAnyware SSC GUI"
#     Kill HBAnyware SSC GUI
      kill $gui_pid > /dev/null 2>&1
   fi
fi


# Kill parent elxdiscoveryd
ppid=`ps -eaf | grep elxdisc | grep -v grep | awk '{ print $2 }' | sort -n | head -1`
if [ ! -z "$ppid" ];then
        echo "Stopping HBAnyware Discovery Server"
	kill $ppid > /dev/null 2>&1
fi

# Kill any remaining child of elxdisc
for ppid in `ps -eaf | grep elxdisc | grep -v grep | awk '{ print $2 }' | sort -n`
do
	kill -9 $ppid > /dev/null 2>&1
done


